import java.util.Scanner;

/**
 * @author gao
 * @date 2025/6/11 14:32
 */

public class Solution {
    public int minPathSum(int[][] grid) {
        // 获取网格的行数和列数
        int m = grid.length;
        int n = grid[0].length;

        // 创建一个二维数组dp，用于存储到达每个位置的最小路径和
        int[][] dp = new int[m + 1][n + 1];

        for (int i = 0; i <= m; i++) {
            dp[i][0] = Integer.MAX_VALUE;
        }

        for (int i = 0; i <= n; i++) {
            dp[0][i] = Integer.MAX_VALUE;
        }

        // 初始化dp数组的起点位置，表示从起点到起点的最小路径和为0
        dp[0][1] = dp[1][0] = 0;

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                // 到达当前位置的最小路径和等于到达上方位置和左方位置的最小路径和加上当前位置的值
                dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1];
            }
        }
        return dp[m][n];
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int m = in.nextInt();
            int n = in.nextInt();
            int[][] grid = new int[m][n];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    grid[i][j] = in.nextInt();
                }
            }
            Solution solution = new Solution();
            System.out.println(solution.minPathSum(grid));
        }
    }
}
